package cn.edu.cug.cs.gtl.plugins.catalog.tree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.AbstractAction;
import cn.edu.cug.cs.gtl.apis.ui.catalogtree.CatalogTree;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.DataCatalog;
import cn.edu.cug.cs.gtl.utils.postgresql.DataCatalogManager4Postgresql;
import cn.edu.cug.cs.gtl.utils.postgresql.PostgresqlUtil;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.xml.crypto.Data;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;

public class EditSelectCatalogAction extends AbstractAction {
    @Override
    public void initialize(AbstractApplication theApp) {
        this.theApp = theApp;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());

        name = resourceBundle.getString("popupmenu.catalog.tree.edit_select_catalog");
        menuItem.setText(name);
        menuItem.addActionListener(this);
        ((CatalogTree) (theApp.getMainFrame().getCatalogPanel().getTreeComponent())).getCatalogNodePopupMenu().add(menuItem);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CatalogTree tree = (CatalogTree) theApp.getMainFrame().getCatalogPanel().getTreeComponent();
        //获取当前选中节点的路径
        TreePath selectionPath = tree.getSelectionPath();
        if (selectionPath != null)   //如果路径不为空，则设置该路径的最后一个节点为可编辑状态
            tree.startEditingAtPath(selectionPath);
        DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

        try {
            ActionTools.Rename(tree, selectNode);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
}
